package try01;

import java.util.LinkedList;
import java.util.List;

public class SpiralMatrixII {
	public void tester() {
		int [][] a = generateMatrix(0);
//		System.out.println(a[0][0]);
	}
	
	public int [][] generateMatrix(int n) {
		int [][] result = new int [n][n];
		if(n == 0) return result;
		
		s(result);
		return result;
	}
	
	public void s(int[][] matrix) {
        int r1 = 0, r2 = matrix.length - 1;
        int c1 = 0, c2 = matrix[0].length - 1;
        
        int r = 0, c = 0;
        
        int count = 1;
        
        while(r1 <= r2 && c1 <= c2) {
        	while(r1<=r2 && c <= c2) {
        		matrix[r][c] = count;
        		count++;
            	c++;
            }
            r1++;
            c--;
            r++;
            
            while(c1<=c2 && r <= r2) {
            	matrix[r][c] = count;
        		count++;
            	r++;
            }
            r--;
            c2--;
            c--;
            
            while(r1<=r2 && c >= c1) {
            	matrix[r][c] = count;
        		count++;
            	c--;
        	}
        	c++;
        	r2--;
        	r--;
            
        	while(c1<=c2 && r >= r1) {
        		matrix[r][c] = count;
        		count++;
            	r--;
            }
            r++;
            c1++;
            c++;
            
//            System.out.println(r1+" "+r2+" "+c1+" "+c2);
        }
    }
}
